#!/usr/bin/python


# I.D.E.A. model :D

# I - imports and initialize
import os, sys
import pygame
from pygame.locals import *
from pygame import Rect, Color


from obj import *

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

pygame.init()


# D - display configuration

screen = pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption("Tower Defense")
pygame.mouse.set_visible(False)


# E - Entities

# image filenames
BALL_IMAGE_FILENAME  = 'images/ball.gif'
GRASS_IMAGE_FILENAME = 'images/grass.jpg'
BLACK_IMAGE_FILENAME = 'images/black.jpg'
MOUSE_IMAGE_FILENAME = 'images/mouse.gif'

TOWER_0_IMAGE_FILENAME = 'images/tower0.gif'
SHOT_0_IMAGE_FILENAME = 'images/bala0.gif'

#image surfaces loading
ball_image  = pygame.image.load(BALL_IMAGE_FILENAME).convert_alpha()
grass_image = pygame.image.load(GRASS_IMAGE_FILENAME).convert()
black_image = pygame.image.load(BLACK_IMAGE_FILENAME).convert()
mouse_image = pygame.image.load(MOUSE_IMAGE_FILENAME).convert_alpha()

tower0_image = pygame.image.load(TOWER_0_IMAGE_FILENAME).convert_alpha()
shot0_image = pygame.image.load(SHOT_0_IMAGE_FILENAME).convert_alpha()


# map creation
monster_images = [ ball_image ]
tower_images = [ (tower0_image, shot0_image) ]
grid = grid(screen, tower0_image)
mapa = Map(screen,grass_image, monster_images, tower_images, grid)


#A - Action (broken into A.L.T.E.R. steps)


#A - Assign values to key variables
clock = pygame.time.Clock()

pre_game_timer = Alarm(2000,mapa.game_start) # start game at second 2

# TODO: implement level change
# level_timer = Alarm(2000*23, mapa.start_next_wave) # change level each 23 secs



#L - Set up main loop
while True:
	
	#T - Timers and clock
	time_passed = clock.tick(50)	# in milliseconds
	
	# update game timers
	pre_game_timer.update(time_passed)
	#level_timer.update(time_passed)
	
	
	#E - Event handling and processing
	for event in pygame.event.get():
		if event.type == QUIT:
			exit(0)
		elif event.type == KEYDOWN and event.key == K_ESCAPE:
			exit(0)
			
		elif event.type == KEYDOWN and event.key == K_t:
			mapa.buy_tower(type_num = 0)
			
		elif event.type == MOUSEBUTTONDOWN:			#aca hay un bug! no quuise tocar mucho, pero si vos cliqueas sin tocar 't' y dps 
			x,y = pygame.mouse.get_pos()			#queres poner una torre en el mismo lugar no te deja. 
									#creo q no es de lo q escribi yo asiq no quise tocar mucho
			a,b = grid.getpos(x,y)			
			if event.button == 1 and grid.test(a,b) == True:
				mapa.place_tower()
			else:
				print 'No se puede ubicar la torre en esa posicion...'
	
	x,y = pygame.mouse.get_pos()
	

		
	mapa.creep_spawner_timer.update(time_passed)
	
	# update (change internal variables and states) each monster
	for monster in mapa.monsters:
		monster.update()
		
	# update towers	
	for tower in mapa.towers:
		# if monster in range, shoot him!
		for monster in mapa.monsters:
			if monster.alive and \
			(monster.x - tower.x)** 2 + (monster.y - tower.y) **2  < tower.range **2 :
				if tower.shooting == False:
					tower.attack(monster)
				break
		# then update variables
		tower.update(time_passed)
		
		
	
	
	#R - Refresh display	
	screen.fill((0,0,0))
	mapa.draw()
	draw_rimmed_box(screen,
			Rect((screen.get_width()-190,10,
				180,screen.get_height()-20)),
			Color('gray'),
			10)
			
			
	# draw monsters
	for monster in mapa.monsters:
		monster.draw()
	# draw towers
	for tower in mapa.towers:
		tower.draw()
	
	# draw cursor
	screen.blit(mouse_image,(x,y))
	
	# display everything in screen (all previous draws where hidden!)
	pygame.display.flip()
